﻿            /* Write a program that finds the maximal sequence of equal elements in an array.
		            Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1}  {2, 2, 2}.
            */

using System;
using System.Collections.Generic;

class MaxSq
{
    static void Main()
    {
        //make new list
        List<int> nums = new List<int>();

        string str = null, stack = null;
        int N;
        //input values
        Console.WriteLine("\tEnter elements (press enter to stop): ");
        
        //loop while user hits enter
        while (str != "")
        {
            
            //parse properly invalid input values
            bool iProvided = false;
            do
            {
                if (iProvided == true) Console.WriteLine("Error: invalid input entered, please enter an integer N: ");
                else iProvided = true;
                str = Console.ReadLine();
            } while (((Int32.TryParse(str, out N) == false) || iProvided == false) && str != "");
            
            //everything's ok, add this value to list N
            nums.Add(N);
        }
        nums.Remove(nums[nums.Count-1]); //remove the last (zero) element which occurs because of hitting enter 
                                         //(we could escape this easily by a do-while cycle, however this one looks neater to me

        //we have to have three variables - the current equals which is being used as a stack, the last equals number which
        //will be overrided in case the current stack has a higher value
        //and the startIndex of our equals values
        int? CurrentCountEquals = 0, lastCountEquals = 0, startIndex = null;
        for (int i = 1; i < nums.Count; i++)
        {
            if (nums[i] == nums[i - 1]) //check if previous element is same as current, increase CurrentCountEquals with 1 if so
            {
                CurrentCountEquals++;
                if (CurrentCountEquals > lastCountEquals)
                {
                    lastCountEquals = CurrentCountEquals;
                    startIndex = i - CurrentCountEquals; //calculate the starting index by substracting it from the CurrentCountEqualsStack
                                                         //(so we have a starting index from the first element from the equal values)
                }
            }
            else
            {
                CurrentCountEquals = 0; //if we have meet a different value, restart the counter stack
            }
        }

        
        //output results
        if (startIndex != null)
        {
            Console.Write(@"
            --------------------------------
            Most equals starting index: {0}
            Values: ", startIndex);
            for (int i = (int)startIndex; i <= startIndex + lastCountEquals; i++)
            {
                Console.Write("{0} ", nums[i]);
            }
            Console.WriteLine();
        }
        else
        {
            Console.WriteLine("No equals!");
        }
    }
}
